package com.astrofizzbizz.stoneedge3.client.imaging;

import com.astrofizzbizz.stoneedge3.client.StoneEdge3;
import com.astrofizzbizz.stoneedge3.shared.AstroTarget;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FindAutoFocusStarPanel extends CaptionPanel
{
	StoneEdge3 stoneEdge3;
	private Button findStarButton = new Button("Find Star");

	public FindAutoFocusStarPanel(StoneEdge3 stoneEdge3)
	{
		super("Auto Focus Star");
		this.stoneEdge3 = stoneEdge3;
		VerticalPanel vp1  = new VerticalPanel();
		
		vp1.add(findStarButton);
		findStarButton.addClickHandler(new FindStarButtonClickHandler());
		setContentWidget(vp1);
		
	}
	private class FindStarButtonClickHandler implements ClickHandler
	{
		@Override
		public void onClick(ClickEvent event) 
		{
			stoneEdge3.getStoneEdge3Service().getBestAutoFocusStar(stoneEdge3.getObsLongitude(), 
					stoneEdge3.getObsLatitude(), stoneEdge3.getTelescopeStatusPanel().getUpdateDate(), 
					stoneEdge3.isDebug(), new FindBestAutoFocusStarAsyncCallback());
			
		}
		
	}
	private class FindBestAutoFocusStarAsyncCallback implements AsyncCallback<AstroTarget>
	{
		@Override
		public void onFailure(Throwable caught) 
		{
			stoneEdge3.getStatusTextArea().addStatus("Error: on Test Button.");
		}
		@Override
		public void onSuccess(AstroTarget result) 
		{
			stoneEdge3.getStatusTextArea().addStatus(result.getName());
			stoneEdge3.getSe3TabLayoutPanel().getTargetingPanel().getAstroTargetPanel().setAstroTarget(new AstroTarget(result));
			stoneEdge3.getSe3TabLayoutPanel().getTargetingPanel().getAstroTargetPanel().setTargetInfo();
		}
		
	}


}
